
import { EStatus } from "../common/EnumType";
import { BaseBehavior } from "./BaseBehavior";
import { BaseDecorator } from "./BaseDecorator";
import { BaseCondition } from "./BaseCondition";

export abstract class BaseBehaviorNode<T = any> extends BaseBehavior<T> {
    protected conditions: Array<BaseCondition> = [];
    protected decorators: Array<BaseDecorator> = [];

    public addCondition(condition: BaseCondition) {
        this.conditions.push(condition);
    }

    public addDecorator(decorator: BaseDecorator) {
        this.decorators.push(decorator);
    }

    public onTickConditions(): EStatus {
        let status = EStatus.Success;
        for (let i = 0; i < this.conditions.length; i++) {
            status = this.conditions[i].onTick();
            if (status == EStatus.Failure) {
                break;
            }
        }
        return status;
    }

    public onTickDecorators(): EStatus {
        let status = this.status;
        for (let i = 0; i < this.decorators.length; i++) {
            status = this.decorators[i].onTick();
        }
        return status;
    }

    public reset(): void {
        super.reset();

        for (let i = 0; i < this.conditions.length; i++) {
            this.conditions[i].reset();
        }

        for (let i = 0; i < this.decorators.length; i++) {
            this.decorators[i].reset();
        }
    }
}